import { usePublicHooks } from "../../../../../hooks";
import { usePublicFormats } from "../../../../../formats";

import { reactive, onMounted, toRaw } from "vue";
import { delay } from "@pureadmin/utils";
import { staticParkEntry } from "@/api/parking_record/static_park";

export function staticParkEntryConfig() {
  const form = reactive({
    park_code: "",
    park_name: "",
    car_number: "",
    in_time: "",
    car_no_color: -1,
    much_id: "",
    serial_no: "",
    region_code: "",
    order_no: "",
    order_type: 1,
    page: 1,
    limit: 20
  });

  //从父级钩子里取东西
  const { pagination, loadingConfig, dataList, loading } = usePublicHooks();
  const { carColorShow } = usePublicFormats();

  const columns: TableColumnList = [
    {
      label: "车牌号",
      prop: "car_number",
      width: 130,
      fixed: true
    },
    {
      label: "车牌颜色",
      prop: "car_no_color",
      width: 100,
      cellRenderer: ({ row }) => {
        return carColorShow(row.car_no_color);
      }
    },

    {
      label: "停车场名称",
      prop: "park_name",
      width: 200
    },
    {
      label: "入场时间",
      prop: "in_time",
      width: 200
    },
    {
      label: "入场照片",
      slot: "image",
      width: 200
    },
    {
      label: "流水号",
      prop: "serial_no",
      width: 250
    },
    {
      label: "区域编码",
      prop: "region_code",
      width: 200
    },
    {
      label: "订单号",
      prop: "order_no",
      width: 300
    },
    {
      label: "商户编号",
      prop: "much_id",
      width: 300
    }
  ];

  async function onSearch() {
    loading.value = true;
    const { data } = await staticParkEntry(toRaw(form));
    dataList.value = data.list;
    pagination.total = data.total;
    pagination.pageSize = data.pageSize;
    pagination.currentPage = data.currentPage;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  //页面加载
  onMounted(() => {
    onSearch();
  });

  //页面条数变化
  function onSizeChange(val) {
    form.limit = val;
    onSearch();
  }

  //页码变化
  function onCurrentChange(val) {
    loadingConfig.text = `正在加载第${val}页...`;
    loading.value = true;
    delay(600).then(() => {
      form.page = val;
      onSearch();

      //刷新数据
      loading.value = false;
    });
  }

  //重置表单
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  return {
    columns,
    form,
    loading,
    onSearch,
    resetForm,
    dataList,
    pagination,
    onSizeChange,
    onCurrentChange,
    loadingConfig
  };
}
